package com.blockmeta.bbs.businesslibrary.base.fragment;

import android.view.LayoutInflater;
import android.view.View;

import com.blockmeta.bbs.baselibrary.net.callback.Callback;
import com.blockmeta.bbs.baselibrary.utils.loading.ProgressBarUtil;
import com.blockmeta.bbs.businesslibrary.R;
import com.blockmeta.bbs.businesslibrary.constant.Constant;
import com.blockmeta.bbs.businesslibrary.net.okhttp.AdapterLoadDataBusiness;
import com.blockmeta.bbs.businesslibrary.net.okhttp.OKHttpGetReq;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.HashMap;

/**
 * Created by author on 2018/5/8.
 * 因为列表页 大多数请求都是类似的 因为可以进一步深度抽取
 * 只需要实例化到每一个页面的adapter
 * 具体业务bean 分页参数deng
 */

public abstract class BaseLazyBusinessListFragment<T> extends BaseLazyRefreshListFragment {
    public T mPOJO;
    public OKHttpGetReq mGetReq;


    public BaseQuickAdapter mAdapter;
    /**
     * 设置分页参数
     */
    public String pageNo;

    public String getPageNo() {
        if (pageNo == null) {
            pageNo = "";
        }
        return pageNo;
    }

    public String getPageNo(String defaultPageNo) {
        if (pageNo == null) {
            pageNo = defaultPageNo;
        }
        return pageNo;
    }

    public void setPageNo(String pageNo) {
        this.pageNo = pageNo;
    }

    @Override
    protected void initializationData() {
        ProgressBarUtil.showProgress(mContext);
        mAdapter = initAdapter();

        if (addHeaderView() != null) {
            mAdapter.addHeaderView(addHeaderView());
        }

        list_recy.setAdapter(mAdapter);
        initNet(false);
    }

    public View addHeaderView() {
        return null;
    }


    public void initNet(final boolean isLoadMore) {
        mGetReq = new OKHttpGetReq(mContext, getUrl());
        mGetReq.getDataWithParam(new Callback() {
            @Override
            public void onError(String resp) {
                ProgressBarUtil.dismissProgress(mContext);
                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
                AdapterLoadDataBusiness.LoadErrorData(mContext, mAdapter, list_recy, new AdapterLoadDataBusiness.ReLoadNet() {
                    @Override
                    public void retryLoadNet() {
                        initNet(false);
                    }
                });
            }

            @Override
            public void onResponse(String resp, int id) {
                ProgressBarUtil.dismissProgress(mContext);
                AdapterLoadDataBusiness.finishLoadAndRefresh(list_swipe);
                processData(resp, isLoadMore);
            }
        }, initHashMap());

    }

    public void processData(String resp, boolean isLoadMore) {
        try {
            mPOJO = parseData(resp);
            getDebateData(isLoadMore);
        } catch (Exception e) {
            e.printStackTrace();
            mPOJO = null;
        }
        View empty;
        if (getArguments() != null && getArguments().getBoolean(Constant.THREE)){
            empty =  LayoutInflater.from(mContext).inflate(R.layout.empty_search_for_im, null);
        } else if (getArguments() != null && "keyword".equals(getArguments().getString(Constant.ZERO))) {
            empty = LayoutInflater.from(mContext).inflate(R.layout.empty_search, null);
        } else if (getArguments() != null && "keys".equals(getArguments().getString(Constant.ZERO))) {
            empty = LayoutInflater.from(mContext).inflate(R.layout.empty_search, null);
        } else {
            empty = LayoutInflater.from(mContext).inflate(R.layout.empty_data, null);
        }
        mAdapter.setHeaderFooterEmpty(true, true);
        mAdapter.setEmptyView(empty);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                setAdapterClick(adapter, view, position);
            }
        });
        if (mPOJO!=null){
            setHeaderClick();
        }

        if (isLoadMore && mPOJO != null) {
            adapterAddData();
        } else if (mPOJO != null) {
            adapterSetData();
        }
    }

    protected void getDebateData(boolean isLoadMor){

    }
    public void setHeaderClick() {

    }

    /**
     * 处理页面中的点击事件
     *
     * @param adapter
     * @param view
     * @param position
     */
    public void setAdapterClick(BaseQuickAdapter adapter, View view, int position) {

    }


    /**
     * @return 实例化adapter
     */
    public abstract BaseQuickAdapter initAdapter();

    public abstract String getUrl();

    public abstract HashMap<String, String> initHashMap();

    /**
     * @return 实例化 Bean数据
     */
    public abstract T parseData(String resp);

    /**
     * 初始化以及下拉刷新设置数据
     */
    protected abstract void adapterSetData();

    /**
     * 加载更多数据
     */
    protected abstract void adapterAddData();


}
